<?php
      include_once('../src/classes/class.mysql.php');  
      session_start();
      $db = new MySQL();

      $comtec_id_order      = $_GET['id_order'];
      $type                 = $_GET['type'];
      $token                = $_GET['token'];
     
      $result = $db->fetch_array($db->query("
       SELECT o.comtec_id_machine,m.cod_mainteiner,o.comtec_id_responsible,m.comtec_id_configman,cod_order,operation_type,o.comtec_id_mainteiner
       FROM orders AS o INNER JOIN machines AS m ON o.comtec_id_machine = m.comtec_id_machine 
       WHERE comtec_id_order = $comtec_id_order"
      ));
      
      $cod_mainteiner = $result['cod_mainteiner'];
      $operation_type = $result['operation_type'];
      $comtec_id_machine = $result['comtec_id_machine'];
      $comtec_id_responsible = $result['comtec_id_responsible'];
      $comtec_id_configman = $result['comtec_id_configman'];
      $cod_order = $result['cod_order'];
      $comtec_id_mainteiner = $result['comtec_id_mainteiner'];
     
      date_default_timezone_set('America/Santiago');
      $op_day = date("d");
      $op_month = date("m");
      $op_year = date("Y");
      $date = $op_year.$op_month.$op_day;
     
      $stmt = $db->query("SELECT ADDDATE('$date',INTERVAL maintainment_period DAY) as date_next_maintenance FROM configman WHERE comtec_id_configman=$comtec_id_configman");
      $row = mysql_fetch_array($stmt, MYSQL_NUM);  
      $date  = $row[0];
      $next_day = substr($date,8,2);
      $next_month = substr($date,5,2);
      $next_year = substr($date,0,4);
     
     function extra($x){
      if ($x <= 9){
       $x = "0".$x;   
      }   
      return $x;   
     }
?>    
<fieldset class="curved">
 <legend>Ingresar</legend>
 <table style="margin: 10px auto; border-spacing: 10px;">
        <tr>
            <td width="200"><label>M&aacute;quina</label></td>
            <td><input type="text" DISABLED size="40" value="<?=$cod_mainteiner;?>"/></td>
            <td width="200"><label>Orden de trabajo</label></td>
            <td><input type="text" DISABLED size="40" value="<?=$cod_order;?>"/></td>
        </tr>
        <tr>
            <td width="200"><label>Tipo de operaci&oacute;n</label></td>
            <td>
                <select id="operation_type" onchange="javascript:operation_type()" style="width:257px;">
                       <?if($operation_type==0){?>
                       <option value="0" selected>Selecci&oacute;n...</option>
                       <?}else{?><option value="0">Selecci&oacute;n...</option><?}?>
                       <?if($operation_type==1){?>
                       <option value="1" selected>Mantenci&oacute;n</option>
                       <?}else{?><option value="1">Mantenci&oacute;n</option><?}?>
                       <?if($operation_type==2){?>
                       <option value="2" selected>Emergencia</option>
                       <?}else{?><option value="2">Emergencia</option><?}?>
                       <?if($operation_type==3){?>
                       <option value="3" selected>Instalaci&oacute;n</option>
                       <?}else{?><option value="3">Instalaci&oacute;n</option><?}?>
                       <?if($operation_type==4){?>
                       <option value="4" selected>Reparaci&oacute;n</option>
                       <?}else{?><option value="4">Reparaci&oacute;n</option><?}?>
                </select>
           </td>
           <td width="200"><label>Encargado<label></td>
           <td>
               <select id="comtec_id_responsible" style="width:257px;">
                <option value="0" selected>Selecci&oacute;n...</option>
                    <?
                        $responsible = $db->query("SELECT comtec_id_responsible, name, lastname1, lastname2, status_reg FROM responsible WHERE comtec_id_mainteiner = $comtec_id_mainteiner ");
                        while ($r = mysql_fetch_array($responsible, MYSQL_ASSOC))
                        {
                            if ($r['status_reg'] == 1)
                            {
                             if ($comtec_id_responsible==$r['comtec_id_responsible']){
                             echo "<option value=".$r['comtec_id_responsible']." selected>".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";
                             }else {
                              echo "<option value=".$r['comtec_id_responsible'].">".$r['name']." ".$r['lastname1']." ".$r['lastname2']."</option>";    
                             }
                             
                            }
                        }
                    ?>
               </select>
            </td>
           </tr>
           <tr>
               <td width="200"><label>Diagn&oacute;stico</label></td>
               <td colspan=4><textarea id="diagnosis" rows="2" cols="91" style="resize:none"></textarea></td>
           </tr>
           <tr>
               <td width="200"><label>Repuestos cambiados</label></td>
                <td colspan=4><textarea id="changed_parts" rows="2" cols="91" style="resize:none"></textarea></td>
           </tr>
           <tr>
                <td width="200"><label>Notas</label></td>
                <td colspan=4><textarea id="notes" rows="2" cols="91" style="resize:none"></textarea></td>
           </tr>
           <tr id="next_date" style="display: none">
               
               <td width="200"><label>Fecha prox. Mantenci&oacute;n</label></td>
               <td>
                  <?
                  $optionsXML = simplexml_load_file("../xml/days.xml"); 
                  $options =  $optionsXML->xpath("//day/text()");
                  echo '<select id="next_day">';
                  while(list( $key , $value) = each($options)) {                       
                   if ($next_day == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/months.xml"); 
                  $options =  $optionsXML->xpath("//month/text()");
                  echo '<select id="next_month">';
                  while(list( $key , $value) = each($options)) {  
                   if ($next_month == $key) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/years.xml"); 
                  $options =  $optionsXML->xpath("//year/text()");
                  echo '<select id="next_year">';
                  while(list($key,$value) = each($options)) {   
                   if ($next_year == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                   else {echo '<option value='.$value.'>' . $value . '</option>';}
                  }
                  echo '</select>';
                  ?>
               </td>
               
           </tr>
           <?if(($_SESSION["users"] != "technicians")&&($_SESSION["users"] != "administratives-1")){?>
           <tr>
                <td width="200"><label>Valor mano de obra</label></td>
                <td><input type="text" id="value_work" size="40" value="0" onchange="javascript:addValue()" /></td>
                <td width="200"><label>Valores repuestos</label></td>
                <td><input type="text" id="value_parts" size="40" value="0" onchange="javascript:addValue()" /></td>
           </tr>
           <tr>
                <td width="200"><label>Otros valores</label></td>
                <td><input type="text" id="value_others" size="40" value="0" onchange="javascript:addValue()" /></td>            
                <td width="200"><label>Valor total</label></td>
                <td><input type="text" size="40" id="value_total" DISABLED value="0" /></td>
           </tr>
           <?}?>
           <tr>              
               <td width="200"><label>Fecha de inicio</label></td>
               <td>
                   <?
                     $optionsXML = simplexml_load_file("../xml/days.xml"); 
                     $options =  $optionsXML->xpath("//day/text()");
                     echo '<select id="op_day1" onchange="javascript:setDelay()">';
                     while(list( $key , $value) = each($options)) {      
                     if ($op_day == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                     else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                     }
                     echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      echo '<select id="op_month1" onchange="javascript:setDelay()">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      echo '<select id="op_year1" onchange="javascript:setDelay()">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                       else {echo '<option value='.$value.'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
               </td>
               <td width="200"><label>Hora de inicio</label></td>
               <td>
                  <?
                  $optionsXML = simplexml_load_file("../xml/hours.xml"); 
                  $options =  $optionsXML->xpath("//hour/text()");
                  echo '<select id="hour1" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {   
                   echo '<option value='.$value.'>' . $value . '</option>';
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/minutes.xml"); 
                  $options =  $optionsXML->xpath("//minute/text()");
                  echo '<select id="minute1" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {   
                   echo '<option value='.$value.'>' . $value . '</option>';
                  }
                  echo '</select>';
                  ?> 
               </td>
           </tr>   
           <tr>
               <td width="200">
                   <label>Fecha de fin</label>
               </td>
               <td>
                   <?
                     $optionsXML = simplexml_load_file("../xml/days.xml"); 
                     $options =  $optionsXML->xpath("//day/text()");
                     echo '<select id="op_day2" onchange="javascript:setDelay()">';
                     while(list( $key , $value) = each($options)) {      
                     if ($op_day == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                     else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                     }
                     echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/months.xml"); 
                      $options =  $optionsXML->xpath("//month/text()");
                      echo '<select id="op_month2" onchange="javascript:setDelay()">';
                      while(list( $key , $value) = each($options)) {  
                       if ($op_month == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
                       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
                  <?
                      $optionsXML = simplexml_load_file("../xml/years.xml"); 
                      $options =  $optionsXML->xpath("//year/text()");
                      echo '<select id="op_year2" onchange="javascript:setDelay()">';
                      while(list($key,$value) = each($options)) {   
                       if ($op_year == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
                       else {echo '<option value='.$value.'>' . $value . '</option>';}
                      }
                      echo '</select>';
                  ?>
               </td>
               <td width="200">
                   <label>Hora de fin</label>
               </td>   
               <td>
                  <?
                  $optionsXML = simplexml_load_file("../xml/hours.xml"); 
                  $options =  $optionsXML->xpath("//hour/text()");
                  echo '<select id="hour2" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {   
                   echo '<option value='.$value.'>' . $value . '</option>';
                  }
                  echo '</select>';
                  ?>
                  <?
                  $optionsXML = simplexml_load_file("../xml/minutes.xml"); 
                  $options =  $optionsXML->xpath("//minute/text()");
                  echo '<select id="minute2" onchange="javascript:setDelay()">';
                  while(list($key,$value) = each($options)) {   
                   echo '<option value='.$value.'>' . $value . '</option>';
                  }
                  echo '</select>';
                  ?> 
               </td> 
           </tr>
          <? if($_SESSION['users']=="administratives-2" || $_SESSION['users']=="managers"){  ?> 
           <tr>
               <td width="200"><label>Demora</label></td>
               <td><input type="text" DISABLED id="delay" value="0" > minutos</td>
           </tr>
           <?}?>
           <? if($_SESSION['users']!="technicians" && $_SESSION['users']!="clients"){ ?>
           <tr>              
               <td width="200"><label>Notas para cliente</label></td>
               <td colspan=4><textarea id="notes_for_owner" rows="2" cols="91" style="resize:none"></textarea></td>
           </tr>
           <?}?>
           <tr>
                <td>
                    <input type="button" value="Guardar" onclick="javascript:saveOperation()" />
                    <?php if($type == 1){ ?>
                     <input type="button" value="Cancelar" onclick="javascript:loadOrders(<?=$comtec_id_machine?>)" />
                    <? }elseif($type == 2){ ?>
                     <input type="button" value="Cancelar" onclick="javascript:loadHome()" />
                    <? }elseif($type == 4){ ?>
                     <input type="button" value="Cancelar" onclick="javascript:goBackSearch()" />
                    <? }?>
                </td>    
           </tr>
          </table>
  <input type="hidden" id="comtec_id_machine" value="<?=$comtec_id_machine?>" />
  <input type="hidden" id="comtec_id_order" value="<?=$comtec_id_order?>" />
  <input type="hidden" id="type_operation" value="<?=$type?>" />
  <input type="hidden" id="token" value="<?=$token?>" />
  <div id="result_add"></div> 
</fieldset>
